package com.ntv.service.user;

import com.ntv.domain.IUser;
import com.ntv.persistent.PersistentException;
import com.ntv.persistent.user.IUserDao;
import com.ntv.service.DomainException;

public class UserAppService  implements IUserAppService {
	
	private IUserDao userDao;
	
	
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	
	public IUser findUserById(Long userId) throws DomainException {
		try {
			return userDao.findUserByID(userId);
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public IUser findUserByUserName(String username) throws DomainException {
		try {
			return userDao.findUserByName(username);
		} catch (PersistentException e) {
			throw new DomainException(e);
		}

	}

	public IUser getUser(String email, String password) throws DomainException{
		try {
			return userDao.findUser(email, password);
		} catch (PersistentException e) {
			throw new DomainException(e);
		}
	}

	public IUser registerUserIfValid(IUser user, boolean hasError) throws DomainException{
		return null;
	}
	
	public IUser createUser(IUser user) throws DomainException {
		try {			
			IUser user1 = userDao.findUserByEmail(user.getEmail());
			if (user != null) {
				return userDao.createUser(user);
			}
			else {
				throw new DuplicateUserException("His name is");
			}
		} catch (PersistentException ex) {
			throw new DomainException(ex);
		}
	}
}
